package com.example.muyonghui.mytimetable;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends AppCompatActivity {

    private EditText mobile;

    private EditText password;

    private Button register;

    private MyDatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        dbHelper = new MyDatabaseHelper(this, "UserStore.db", null, 3);

        mobile = (EditText) findViewById(R.id.mobile);
        password = (EditText) findViewById(R.id.password);
        register = (Button) findViewById(R.id.register);

        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String mobiletxt = mobile.getText().toString();
                String passwordtxt = password.getText().toString();
                if (mobiletxt.length() != 11 || TextUtils.isEmpty(mobiletxt)) {
                    Toast.makeText(RegisterActivity.this, "请填写规范的手机号码",
                            Toast.LENGTH_SHORT).show();
                }else if (TextUtils.isEmpty(passwordtxt)){
                    Toast.makeText(RegisterActivity.this, "密码不得为空",
                            Toast.LENGTH_SHORT).show();
                }else{
                    save(mobiletxt, passwordtxt);

                    Toast.makeText(RegisterActivity.this, "已存储",
                            Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent();
                    intent.setClass(RegisterActivity.this, LoginActivity.class);
                    RegisterActivity.this.startActivity(intent);
                }
            }
        });

    }

    public void save(String mobile,String password){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        // 开始组装第一条数据
        values.put("username", mobile);
        values.put("password", password);
        db.insert("User", null, values); // 插入数据
        values.clear();
    }
}
